
// EventNotifyDoc.cpp : implementation of the CEventNotifyDoc class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "EventNotify.h"
#endif

#include "EventNotifyDoc.h"

#include <propkey.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CEventNotifyDoc
IMPLEMENT_DYNCREATE(CEventNotifyDoc, CDocument)

BEGIN_MESSAGE_MAP(CEventNotifyDoc, CDocument)
END_MESSAGE_MAP()


// CEventNotifyDoc construction/destruction

CEventNotifyDoc::CEventNotifyDoc()
	: m_bSound(true)
	, m_iNotifyType(NOTIFY_TYPE_MSGBOX)
	, m_iSoundTimeout(0)
{
	// TODO: add one-time construction code here
	
}

CEventNotifyDoc::~CEventNotifyDoc()
{
}

BOOL CEventNotifyDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	for(int i = 0; i < m_vEvents.GetCount(); i++)
	{
		delete m_vEvents[i];
		m_vEvents[i] = NULL;
	}

	for(int i = 0; i < m_vSounds.GetCount(); i++)
	{
		delete m_vSounds[i];
		m_vSounds[i] = NULL;
	}

	m_vEvents.RemoveAll();
	m_vSounds.RemoveAll();

	m_iNotifyType = NOTIFY_TYPE_MSGBOX;
	m_bSound = true;

	return TRUE;
}




// CEventNotifyDoc serialization

void CEventNotifyDoc::Serialize(CArchive& ar)
{
	int nEventsCount;
	int nSoundsCount;
	CEventParams *pepTmp;
	CSound *psTmp;

	if (ar.IsStoring())
	{
		// TODO: add storing code here
		nEventsCount = m_vEvents.GetCount();
		nSoundsCount = m_vSounds.GetCount();

		ar << m_iNotifyType << m_bSound << m_iSoundTimeout << nEventsCount << nSoundsCount;

		for(int i = 0; i < nEventsCount; i++)
			m_vEvents[i]->Serialize(ar);

		for(int i = 0; i < nSoundsCount; i++)
			m_vSounds[i]->Serialize(ar);
	}
	else
	{
		// TODO: add loading code here
		for(int i = 0; i < m_vEvents.GetCount(); i++)
		{
			delete m_vEvents[i];
			m_vEvents[i] = NULL;
		}

		for(int i = 0; i < m_vSounds.GetCount(); i++)
		{
			delete m_vSounds[i];
			m_vSounds[i] = NULL;
		}

		m_vEvents.RemoveAll();
		m_vSounds.RemoveAll();
		
		ar >> m_iNotifyType >> m_bSound >> m_iSoundTimeout >> nEventsCount >> nSoundsCount;
		
		for(int i = 0; i < nEventsCount; i++)
		{
			pepTmp = new CEventParams;
			pepTmp->Serialize(ar);
			m_vEvents.Add(pepTmp);
		}

		for(int i = 0; i < nSoundsCount; i++)
		{
			psTmp = new CSound;
			psTmp->Serialize(ar);
			m_vSounds.Add(psTmp);
		}
	}
}

#ifdef SHARED_HANDLERS

// Support for thumbnails
void CEventNotifyDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// Modify this code to draw the document's data
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// Support for Search Handlers
void CEventNotifyDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// Set search contents from document's data. 
	// The content parts should be separated by ";"

	// For example:  strSearchContent = _T("point;rectangle;circle;ole object;");
	SetSearchContent(strSearchContent);
}

void CEventNotifyDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = NULL;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != NULL)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CEventNotifyDoc diagnostics

#ifdef _DEBUG
void CEventNotifyDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CEventNotifyDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


// CEventNotifyDoc commands


void CEventNotifyDoc::OnCloseDocument()
{
	// TODO: Add your specialized code here and/or call the base class
	for(int i = 0; i < m_vEvents.GetCount(); i++)
	{
		delete m_vEvents[i];
		m_vEvents[i] = NULL;
	}

	for(int i = 0; i < m_vSounds.GetCount(); i++)
	{
		delete m_vSounds[i];
		m_vSounds[i] = NULL;
	}

	m_vEvents.RemoveAll();
	m_vSounds.RemoveAll();

	CDocument::OnCloseDocument();
}
